package com.iflytek.crvoice2txt.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.iflytek.crvoice2txt.R;
import com.iflytek.crvoice2txt.adapter.CommonListViewAdapter;
import com.iflytek.crvoice2txt.adapter.ListViewHolder;
import com.iflytek.crvoice2txt.adapter.SearchItemAdapter;
import com.iflytek.crvoice2txt.database.OnDataChanged;
import com.iflytek.crvoice2txt.database.RecordOperation;
import com.iflytek.crvoice2txt.entity.RecorderEntity;
import com.iflytek.crvoice2txt.entity.RecorderResearchEntity;
import com.iflytek.crvoice2txt.util.DateUtil;
import com.iflytek.crvoice2txt.util.FileUtil;
import com.iflytek.crvoice2txt.widget.CustomDatePicker;
import com.iflytek.crvoice2txt.widget.SlideListView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class RecorderSearchActivity extends BaseActivity implements OnDataChanged {

    private boolean isShowCheckBox = false;
    private TextView tv_topbar_title, tv_recorder_search_current_date;
    private SlideListView lv_recorder_search;
    private SearchItemAdapter adapter;
    private List<RecorderResearchEntity> list = new ArrayList<>();
    private RecordOperation operation;
    private LinearLayout ll_top_bar_back;
    private ImageView iv_recorder_search_left, iv_recorder_search_right;
    private LinearLayout ll_recorder_search_nosign;
    private String CURRENT_DATE;
    private CustomDatePicker customDatePicker;
    private static final String BEGIN_FOOT = " 00:00:00", END_FOOT = " 23:59:59", START_DATE = "1990-01-01 00:00";

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {
            case R.id.ll_top_bar_back:
                finish();
                break;
            case R.id.tv_recorder_search_current_date:
                customDatePicker.show(DateUtil.getCurrentDateStr(DateUtil.DF_YYYY_MM_DD));
                break;
            case R.id.iv_recorder_search_left:
                CURRENT_DATE = DateUtil.moveDate(false, CURRENT_DATE);
                refreshListView();
                break;
            case R.id.iv_recorder_search_right:
                if (DateUtil.compare_date(CURRENT_DATE, DateUtil.getCurrentDateStr(DateUtil.DF_YYYY_MM_DD)) == -1) {
                    CURRENT_DATE = DateUtil.moveDate(true, CURRENT_DATE);
                    refreshListView();
                } else {
                    showToast("当前已是最后一天");
                }
                break;
        }
    }

    @Override
    protected void init() {
        CURRENT_DATE = DateUtil.getCurrentDateStr(DateUtil.DF_YYYY_MM_DD);
        operation = new RecordOperation(this);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_recorder_search;
    }

    @Override
    public void initView(View view) {

        ll_recorder_search_nosign = findViewById(R.id.ll_recorder_search_nosign);
        iv_recorder_search_left = findViewById(R.id.iv_recorder_search_left);
        iv_recorder_search_right = findViewById(R.id.iv_recorder_search_right);
        tv_recorder_search_current_date = findViewById(R.id.tv_recorder_search_current_date);
        tv_recorder_search_current_date.setText(CURRENT_DATE);
        ll_top_bar_back = findViewById(R.id.ll_top_bar_back);
        ll_top_bar_back.setVisibility(View.VISIBLE);
        tv_topbar_title = findViewById(R.id.tv_topbar_title);
        tv_topbar_title.setText("记录查询");
        lv_recorder_search = findViewById(R.id.lv_recorder_search);
        adapter = new SearchItemAdapter(this,list);
        lv_recorder_search.setAdapter(adapter);
        customDatePicker = new CustomDatePicker(this, new CustomDatePicker.ResultHandler() {
            @Override
            public void handle(String time) { // 回调接口，获得选中的时间
                String str = time.split(" ")[0];
                tv_recorder_search_current_date.setText(str);
                CURRENT_DATE = str;
                refreshListView();
            }
        }, START_DATE, DateUtil.getCurrentDateStr(DateUtil.DF_YYYY_MM_DD_HH_MM)); // 初始化日期格式请用：yyyy-MM-dd HH:mm，否则不能正常运行
        customDatePicker.showSpecificTime(false); // 不显示时和分
        customDatePicker.setIsLoop(true); // 不允许循环滚动
    }

    private void refreshListView() {
        tv_recorder_search_current_date.setText(CURRENT_DATE);
        Date begin = DateUtil.StrToDate(CURRENT_DATE + BEGIN_FOOT, DateUtil.DF_YYYY_MM_DD_HH_MM_SS);
        Date end = DateUtil.StrToDate(CURRENT_DATE + END_FOOT, DateUtil.DF_YYYY_MM_DD_HH_MM_SS);
        operation.queryByTimeAsyn(begin, end);
    }

    @Override
    public void setListener() {
        ll_top_bar_back.setOnClickListener(this);
        tv_recorder_search_current_date.setOnClickListener(this);
        iv_recorder_search_left.setOnClickListener(this);
        iv_recorder_search_right.setOnClickListener(this);
        lv_recorder_search.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(RecorderSearchActivity.this, RecorderDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putParcelable(getResources().getString(R.string.recoder_entity_flag), list.get(position).recorderEntity);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        adapter.setOnSearchItemOperation(new SearchItemAdapter.OnSearchItemOperation() {
            @Override
            public void onSearchItemDeleted(int position) {
                operation.deleteRealmObject(list.get(position).recorderEntity.dateStr);
            }
        });
    }

    @Override
    public void doBusiness(Context mContext) {

        Date begin = DateUtil.StrToDate(CURRENT_DATE + BEGIN_FOOT, DateUtil.DF_YYYY_MM_DD_HH_MM_SS);
        Date end = DateUtil.StrToDate(CURRENT_DATE + END_FOOT, DateUtil.DF_YYYY_MM_DD_HH_MM_SS);
        operation.queryByTimeAsyn(begin, end);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (operation != null) {
            operation.closeRealm();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onQueryByTimeAsyn(List<RecorderEntity> list) {
        this.list.clear();
        if (list != null && list.size() != 0) {
            ll_recorder_search_nosign.setVisibility(View.GONE);
            lv_recorder_search.setVisibility(View.VISIBLE);
            for (RecorderEntity recorderEntity : list) {
                RecorderResearchEntity recorderResearchEntity = new RecorderResearchEntity();
                recorderResearchEntity.isChecked = false;
                recorderResearchEntity.recorderEntity = recorderEntity;
                this.list.add(recorderResearchEntity);
            }
        } else {
            lv_recorder_search.setVisibility(View.GONE);
            ll_recorder_search_nosign.setVisibility(View.VISIBLE);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onDeleteSuccess() {
        showToast("删除成功");
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onDeleteFailed() {
        showToast("删除失败");
        adapter.notifyDataSetChanged();
    }
}
